<template>
  <section>
     
    <!-- 面包屑 -->
      <breadcrumbs></breadcrumbs>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="城市名称"
                    prop="name">
        <el-input v-model="searchForm.name"
                  placeholder="请输入城市名称"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.state"
                   placeholder="请选择状态">
          <el-option label="启用"
                     value="1"></el-option>
          <el-option label="禁用"
                     value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <btns @exec="exec"></btns>
    <div class="data-container scroll-table">
      <div>
        <div class="ind-title lh50 f16">城市列表</div>
      </div>
      <!-- 按钮列表 -->
      <el-table :data="tableData"
                border
                style="width: 100%"
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="40">
        </el-table-column>
        <el-table-column prop="cityname"
                         label="城市名称(省)">
        </el-table-column>
        <el-table-column prop="enable"
                         label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.enable==1">启用</div>
            <div v-else>禁用</div>
          </template>
        </el-table-column>
        <el-table-column prop="addtime"
                         label="创建时间">
        </el-table-column>
      </el-table>
      <el-pagination class="mgt-10 pd-0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="searchForm.current"
                     :page-sizes="[10, 20, 30, 50]"
                     :page-size="searchForm.size"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="searchForm.totalCount">
      </el-pagination>
    </div>
    <!--新增修改界面-->
    <el-dialog :title="funTit=='add'?'新增':'修改'"
               :inline="true"
               :visible.sync="addORUpdateVisible"
               width="500px"
               :close-on-click-modal="false">
      <el-form :model="addForm"
               label-width="80px"
               :rules="rules"
               ref="addForm">
        <el-form-item label="城市名称">
          <el-cascader v-model="addForm.city"
                       :options="city"
                       @change="handleChange"></el-cascader>
          <!-- <el-select v-model="searchForm.status"
                     placeholder="请选择城市">
            <el-option :label="item.regionName"
                       v-for="(item,index) in  city"
                       :key="index"
                       :value="item.regionId"></el-option>
          </el-select> -->
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click.native="addORUpdateVisible = false">取消</el-button>
        <el-button type="primary"
                   @click.native="submit('addForm')">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

import btns from '@/components/btns.vue'
import city from "@/utils/city.json"
import breadcrumbs from "@/components/breadcrumbs.vue";

export default {
  components: {
    btns,
    breadcrumbs
  },
  data () {
    return {
      qiyong: "",
      addForm: {},
      cityData: [],
      city: [],
      searchForm: {
        name: '',
        state: '',
        curr: 1,
        size: 10,
        enable: '',

      },
      addORUpdateVisible: false,
      funTit: '',
      cityForm: {
        cityName: "",
        cityId: ''
      },
      rules: {
        "btnIcon": [],
        "btnName": [
          { required: true, message: '请输入城市名称', trigger: 'blur' }
        ],
        "btnCode": [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ],
        "sort": [
          { required: true, message: '排序', trigger: 'blur' }
        ]
      },
      tableData: [],
      tableSelection: []
    }
  },
  created () {
    this.getTableDataList(this.searchForm.current, this.searchForm.size);
    this.cityData = city;
    this.cityData.forEach((item) => {
      let obj = {};
      let children = [];
      if (item.child != undefined || item.child != []) {
        item.child.forEach((op) => {
          let objchild = {
            value: op.regionId,
            label: op.regionName,
            lng: op.regionLng,
            lat: op.regionLat,
          }
          children.push(objchild)
        })
        obj = {
          value: item.regionId,
          label: item.regionName,
          lng: item.regionLng,
          lat: item.regionLat,
          children: children
        }
      }
      this.city.push(obj)
      // console.log(item)
    })
    console.log(this.city, 11111)
  },
  methods: {
    handleSizeChange (v) {
      console.log(v)
      this.searchForm.size = v;
      this.getTableDataList(this.searchForm.current, v, this.searchForm.name, this.searchForm.state);
    },
    handleCurrentChange (v) {
      this.searchForm.current = v;
      this.getTableDataList(v, this.searchForm.size, this.searchForm.name, this.searchForm.state);
    },
    handleChange (value) {
      console.log(value);
    },
    getCityList () {
      // this.$api.
    },
    handleReset () {
      this.searchForm.state = "";
      this.$refs.searchForm.resetFields();
      this.getTableDataList(this.searchForm.current, this.searchForm.size)
    },
    search () {
      this.getTableDataList(this.searchForm.current, this.searchForm.size, this.searchForm.name, this.searchForm.state);
    },
    getTableDataList (curr, size, name, state) {
      //获取接口
      this.$api.city.getList({
        current: curr,
        size: size,
        cityname: name,
        enable: state
      }).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {

      this.btnForm = {
        "cityId": "",
        "cityname": "",
        "enable": ""

      }
      this.addORUpdateVisible = true;
    },
    update () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      this.btnForm = this.tableSelection[0];
      this.addORUpdateVisible = true;
    },
    del () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }

    },
    refresh () {
      this.btnlist();
    },
    enable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      this.cityid = this.tableSelection[0].cityid;
      if (demo.status == 1) {
        this.$message.error('该数据已启用');
        return
      }
      this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.city.btnupdate({ cityId: this.tableSelection[0].cityid, enable: 1 }).then(d => {
          if (d.code == 200) {
            // this.btnlist();
            this.$message.success('启用成功');
            this.getTableDataList(this.searchForm.current, this.searchForm.size);
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    disable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      this.qiyong = this.tableSelection[0].enable;
      if (demo.status == 0) {
        this.$message.error('该数据已禁用');
        return
      }
      this.$confirm('此操作将禁用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.city.btnupdate({ cityId: this.tableSelection[0].cityid, enable: 2 }).then(d => {
          if (d.code == 200) {
            this.getTableDataList(this.searchForm.current, this.searchForm.size);
            this.$message.success('禁用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    submit (formName) {
      let currObj = {};
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.addForm.city)
          let children = [];

          if (this.addForm.city.length > 0) {
            this.cityData.forEach((item) => {

              if (item.regionId == this.addForm.city[0]) {
                children = item.child;
                // console.log(item, this.addForm.city[0])
              }
            })
            if (children.length > 0) {
              children.forEach((item) => {
                console.log(111111, item, this.addForm.city[1])
                if (item.regionId == this.addForm.city[1]) {
                  currObj = item;
                }
              })
            }
          }
          console.log(currObj)
          if (this.funTit == 'add') {

            this.$api.city.add({
              cityId: currObj.regionId,
              cityName: currObj.regionName,
              lat: currObj.regionLat,
              lon: currObj.regionLng
            }).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.addForm = {};
                this.$message({
                  type: "success",
                  message: "添加成功"
                })
                this.currObj={
                  regionId:'',
                  regionName:'',
                  regionLat:'',
                  regionLng:''
                }
                // this.$refs.btnForm.resetFields();
                this.getTableDataList(this.searchForm.current, this.searchForm.size);
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            this.$api.city.btnupdate(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}

</script>